/**
 * 商家端服务
 * 提供商家订单管理、商品管理等功能
 */

import http from './api.js'

const merchantService = {
    // ==================== 订单管理 ====================

    /**
     * 获取商家订单列表
     * GET /api/v1/merchant/orders
     */
    getOrders: async (params = {}) => {
        try {
            const response = await http.get('/merchant/orders', params, {
                needAuth: true
            })
            return response
        } catch (error) {
            console.error('获取商家订单列表失败:', error)
            throw error
        }
    },

    /**
     * 获取订单详情
     * GET /api/v1/merchant/orders/{id}
     */
    getOrderDetail: async (orderId) => {
        try {
            const response = await http.get(`/merchant/orders/${orderId}`, {}, {
                needAuth: true
            })
            return response
        } catch (error) {
            console.error('获取订单详情失败:', error)
            throw error
        }
    },

    /**
     * 获取订单统计
     * GET /api/v1/merchant/orders/stats
     */
    getOrderStats: async () => {
        try {
            const response = await http.get('/merchant/orders/stats', {}, {
                needAuth: true
            })
            return response
        } catch (error) {
            console.error('获取订单统计失败:', error)
            throw error
        }
    },

    /**
     * 接单
     * POST /api/v1/merchant/orders/{id}/accept
     */
    acceptOrder: async (orderId) => {
        try {
            const response = await http.post(`/merchant/orders/${orderId}/accept`, {}, {
                needAuth: true,
                showLoading: true,
                loadingText: '接单中...'
            })
            return response
        } catch (error) {
            console.error('接单失败:', error)
            throw error
        }
    },

    /**
     * 拒单
     * POST /api/v1/merchant/orders/{id}/reject
     */
    rejectOrder: async (orderId, reason) => {
        try {
            const response = await http.post(`/merchant/orders/${orderId}/reject`, {
                reason: reason || '商家拒绝接单'
            }, {
                needAuth: true,
                showLoading: true,
                loadingText: '处理中...'
            })
            return response
        } catch (error) {
            console.error('拒单失败:', error)
            throw error
        }
    },

    /**
     * 开始备货
     * POST /api/v1/merchant/orders/{id}/prepare
     */
    prepareOrder: async (orderId) => {
        try {
            const response = await http.post(`/merchant/orders/${orderId}/prepare`, {}, {
                needAuth: true,
                showLoading: true,
                loadingText: '处理中...'
            })
            return response
        } catch (error) {
            console.error('开始备货失败:', error)
            throw error
        }
    },

    /**
     * 开始配送
     * POST /api/v1/merchant/orders/{id}/ship
     */
    shipOrder: async (orderId) => {
        try {
            const response = await http.post(`/merchant/orders/${orderId}/ship`, {}, {
                needAuth: true,
                showLoading: true,
                loadingText: '处理中...'
            })
            return response
        } catch (error) {
            console.error('开始配送失败:', error)
            throw error
        }
    },

    // ==================== 商品管理 ====================

    /**
     * 获取商家商品列表
     * GET /api/v1/merchant/foods
     */
    getFoods: async (params = {}) => {
        try {
            const response = await http.get('/merchant/foods', params, {
                needAuth: true
            })
            return response
        } catch (error) {
            console.error('获取商品列表失败:', error)
            throw error
        }
    },

    /**
     * 获取商品详情
     * GET /api/v1/merchant/foods/{id}
     */
    getFoodDetail: async (foodId) => {
        try {
            const response = await http.get(`/merchant/foods/${foodId}`, {}, {
                needAuth: true
            })
            return response
        } catch (error) {
            console.error('获取商品详情失败:', error)
            throw error
        }
    },

    /**
     * 添加商品
     * POST /api/v1/merchant/foods
     */
    addFood: async (foodData) => {
        try {
            const response = await http.post('/merchant/foods', foodData, {
                needAuth: true,
                showLoading: true,
                loadingText: '添加中...'
            })
            return response
        } catch (error) {
            console.error('添加商品失败:', error)
            throw error
        }
    },

    /**
     * 更新商品
     * PUT /api/v1/merchant/foods/{id}
     */
    updateFood: async (foodId, foodData) => {
        try {
            const response = await http.put(`/merchant/foods/${foodId}`, foodData, {
                needAuth: true,
                showLoading: true,
                loadingText: '更新中...'
            })
            return response
        } catch (error) {
            console.error('更新商品失败:', error)
            throw error
        }
    },

    /**
     * 删除商品
     * DELETE /api/v1/merchant/foods/{id}
     */
    deleteFood: async (foodId) => {
        try {
            const response = await http.delete(`/merchant/foods/${foodId}`, {}, {
                needAuth: true,
                showLoading: true,
                loadingText: '删除中...'
            })
            return response
        } catch (error) {
            console.error('删除商品失败:', error)
            throw error
        }
    },

    /**
     * 更新商品状态（上下架）
     * PUT /api/v1/merchant/foods/{id}/status
     */
    updateFoodStatus: async (foodId, status) => {
        try {
            const response = await http.put(`/merchant/foods/${foodId}/status`, {
                status: status // 1: 上架, 0: 下架
            }, {
                needAuth: true,
                showLoading: true,
                loadingText: '更新中...'
            })
            return response
        } catch (error) {
            console.error('更新商品状态失败:', error)
            throw error
        }
    },

    // ==================== 统计数据 ====================

    /**
     * 获取统计概览
     * GET /api/v1/merchant/statistics/overview
     */
    getStatistics: async (params = {}) => {
        try {
            const response = await http.get('/merchant/statistics/overview', params, {
                needAuth: true
            })
            return response
        } catch (error) {
            console.error('获取统计数据失败:', error)
            throw error
        }
    },

    // ==================== 店铺管理 ====================

    /**
     * 获取店铺信息
     * GET /api/v1/merchant/info
     */
    getMerchantInfo: async () => {
        try {
            const response = await http.get('/merchant/info', {}, {
                needAuth: true
            })
            return response
        } catch (error) {
            console.error('获取店铺信息失败:', error)
            throw error
        }
    },

    /**
     * 更新店铺信息
     * PUT /api/v1/merchant/info
     */
    updateMerchantInfo: async (merchantData) => {
        try {
            const response = await http.put('/merchant/info', merchantData, {
                needAuth: true,
                showLoading: true,
                loadingText: '更新中...'
            })
            return response
        } catch (error) {
            console.error('更新店铺信息失败:', error)
            throw error
        }
    },

    // ==================== 评价管理 ====================

    /**
     * 获取商家评价列表
     * GET /api/v1/merchant/reviews
     */
    getReviews: async (params = {}) => {
        try {
            const response = await http.get('/merchant/reviews', params, {
                needAuth: true
            })
            return response
        } catch (error) {
            console.error('获取评价列表失败:', error)
            throw error
        }
    },

    /**
     * 回复评价
     * POST /api/v1/merchant/reviews/{reviewId}/reply
     */
    replyReview: async (reviewId, replyContent) => {
        try {
            const response = await http.post(`/merchant/reviews/${reviewId}/reply`, {
                reply: replyContent
            }, {
                needAuth: true,
                showLoading: true,
                loadingText: '回复中...'
            })
            return response
        } catch (error) {
            console.error('回复评价失败:', error)
            throw error
        }
    },

    /**
     * 获取评价统计
     * GET /api/v1/merchant/reviews/statistics
     */
    getReviewStatistics: async () => {
        try {
            const response = await http.get('/merchant/reviews/statistics', {}, {
                needAuth: true
            })
            return response
        } catch (error) {
            console.error('获取评价统计失败:', error)
            throw error
        }
    },

    // ==================== 辅助方法 ====================

    /**
     * 格式化订单状态文本
     */
    getOrderStatusText: (status) => {
        const statusMap = {
            'pending': '待接单',
            'paid': '已支付',
            'preparing': '备货中',
            'shipping': '配送中',
            'delivered': '已完成',
            'cancelled': '已取消',
            'refunded': '已退款'
        }
        return statusMap[status] || '未知状态'
    },

    /**
     * 格式化商品状态文本
     */
    getFoodStatusText: (status) => {
        return status === 1 ? '已上架' : '已下架'
    }
}

export default merchantService
